package com.tiantian.quanminxiu.presenter;

import android.util.Log;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.api.uploadService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.AuthenticationView;
import com.tiantian.quanminxiu.view.EditInfoView;

import java.io.File;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Field;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2016/8/11.
 */
public class AuthenticationPresent implements Presenter<AuthenticationView> {

    private AuthenticationView editInfoView;

    @Override
    public void attachView(AuthenticationView view) {
        this.editInfoView = view;
    }

    @Override
    public void detachView() {
        editInfoView = null;
    }


    public void applyenter(int Sex,String RealName,String Phone,String Account_RealName,String Account_CardNo,
                             String Account_BankFullName,String Account_Province,String Account_City,String Account_BankName,
                             String QQ,String Email,String IDCard){
        editInfoView.Loading_Open(true);
        ApplicationBase app = ApplicationBase.get(editInfoView.getContext());
        AnchorService service = app.getAnchorService();
        service.applyenter(UserData.Current.mid,Constants.APP_TYPE,UserData.Current.sign,Sex,
                RealName,Phone,Account_RealName,Account_CardNo,Account_BankFullName,Account_Province,Account_City,
                Account_BankName, QQ,Email,IDCard)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    editInfoView.Loading_Close();
                })
                .subscribe(str -> {
                    editInfoView.uploadAnchorInfo(true,str.optString("message"));
                }, throwable -> {
                    editInfoView.uploadAnchorInfo(false,throwable.getMessage());
//                    CommonUtils.showToask(editInfoView.getContext(), throwable.getMessage());
                });

    }

    //region 主播身份证 useranchorIDCard(String path,boolean isFront)
    public void useranchorIDCard(String path,final boolean isFront) {
        uploadService service = uploadService.Factory.create();
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("Filedata", file.getName(), requestFile);

        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("mid",UserData.Current.mid);
        map.put("sign", UserData.Current.sign);
        map.put("app_t", Constants.APP_TYPE);
        map.put("Front", isFront?"a":"b");//”a”  : 正面  ”b”  : 背面
        editInfoView.Loading_Open(true);
        service.useranchorIDCard(map,body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .finallyDo(()->{
                    editInfoView.Loading_Close();
                })
                .subscribe(str -> {
                    editInfoView.uploadAnchorPicFront(isFront);
                }, throwable -> {
                    CommonUtils.showToask(editInfoView.getContext(), throwable.getMessage());
                });

    }
    //endregion

    //region 主播封面 anchorCover(final String path)
    public void anchorCover(String path) {
        uploadService service = uploadService.Factory.create();
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("Filedata", file.getName(), requestFile);

        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("mid",UserData.Current.mid);
        map.put("sign", UserData.Current.sign);
        map.put("app_t", Constants.APP_TYPE);
        editInfoView.Loading_Open(true);
        service.cover(map,body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .finallyDo(()->{
                    editInfoView.Loading_Close();
                })
                .subscribe(str -> {
                    editInfoView.uploadAnchorPicCover();
                }, throwable -> {
                    CommonUtils.showToask(editInfoView.getContext(), throwable.getMessage());
                });

    }
    //endregion

}
